package com.ymatou.iapi.sellercs.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellercs.parameter.AppLoginBean;
import com.ymatou.iapi.sellercs.service.AppLoginCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtOnlinecs;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;
/**
*App登入
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_AppLogin{
	private static AppLoginBean apploginBean;
	private static AppLoginCall apploginCall;
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("App登入");
	}

	@Before
	public void caseUp() {
		apploginBean=new AppLoginBean();
		apploginCall=new AppLoginCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(apploginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AppLogin_001() {
		Logger.start(true,"App登入成功");
		try {
			//INSERT INTO `ymt_onlinecs` VALUES ('b0931f9e07ca4fc4bdc4f25dab153c80', 3383, 30245493, 'wesper_testnew1', 1, 2, 0, '2017-09-27 10:58:47', '');
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			Logger.comment("记录日志");
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
			apploginBean.setRequestId(reqid);
			apploginBean.setUserId(csid);
			apploginBean.setUserName("test");
			apploginCall.setData(apploginBean);
			apploginCall.callService();		
			
			Logger.verifyIsContains("true", apploginCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", apploginCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			Logger.comment("验证日志包含发送指令10001");
			loglist.forEach(log->Logger.debug(log));
			List<String> filter = loglist.stream().filter(s->s.contains("10001")).collect(Collectors.toList());
			Logger.debug(filter);
			Logger.verifyEquals(true,filter.size()>=1,"验证日志");
			Logger.comment("验证日志包含发送指令10002");
			List<String> filter2 = loglist.stream().filter(s->s.contains("10002")).collect(Collectors.toList());
			Logger.debug(filter2);
			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(null, apploginBean.getUserId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			Logger.verifyEquals(apploginBean.getUserName(), info.getCSName(), "验证csname");
			Logger.verifyEquals(2, info.getLoginStatus(), "验证loginStatus为App在线2");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppLogin_002() {
		Logger.start(true,"不输入userid失败");
		try {
			apploginCall.setData(apploginBean);
			apploginCall.callService();			
			Logger.verifyIsContains("200", apploginCall.getString("Status"), "验证Status");
			Logger.verifyIsContains("false", apploginCall.getString("Result"), "验证IsSuccess");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppLogin_003() {
		Logger.start(true,"不输入username自动获取");
		try {
			Integer csid=30245493;
			Integer mcsid=3383;
			String name=new JUserWapper().selectUserinfoByUid(csid).get(0).getLoginId();
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			apploginBean.setUserId(csid);
//			apploginBean.setUserName("test");
			apploginCall.setData(apploginBean);
			apploginCall.callService();			
			Logger.verifyIsContains("true", apploginCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", apploginCall.getString("Status"), "验证Status");
			Thread.sleep(1000);
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(null, apploginBean.getUserId()).get(0);
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			Logger.verifyEquals(name, info.getCSName(), "验证csname");
			Logger.verifyEquals(2, info.getLoginStatus(), "验证loginStatus为App在线2");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppLogin_004() {
		Logger.start(true,"输入userid为子账号登入成功");
		try {
			Integer csid=zsellerId;
			Integer mcsid=sellerId;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			apploginBean.setUserId(csid);
			apploginBean.setUserName("test");
			apploginCall.setData(apploginBean);
			apploginCall.callService();			
			Logger.verifyIsContains("true", apploginCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", apploginCall.getString("Status"), "验证Status");
			Thread.sleep(1000);
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(null, apploginBean.getUserId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			Logger.verifyEquals(apploginBean.getUserName(), info.getCSName(), "验证csname");
			Logger.verifyEquals(2, info.getLoginStatus(), "验证loginStatus为App在线2");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppLogin_005() {
		Logger.start(true,"输入userid不存在返回失败");
		try {
			apploginBean.setUserId(-999);
			apploginCall.setData(apploginBean);
			apploginCall.callService();			
			Logger.verifyIsContains("200", apploginCall.getString("Status"), "验证Status");
			Logger.verifyEquals(false,apploginCall.getJsonObject().get("Result").getAsJsonObject().get("Success").getAsBoolean(),"验证返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppLogin_006() {
		Logger.start(true,"Pc登入成功后App登入成功");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			String token= TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(mcsid, csid,token);
			Logger.comment("记录日志");
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			String reqid=TestDataManager.getRandomUUID().toString().replaceAll("-", "");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
			apploginBean.setRequestId(reqid);
			apploginBean.setUserId(csid);
			apploginCall.setData(apploginBean);
			apploginCall.callService();		
			
			Logger.verifyIsContains("true", apploginCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", apploginCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			Logger.comment("验证日志包含发送指令10002");
			List<String> filter2 = loglist.stream().filter(s->s.contains("10002")).collect(Collectors.toList());
			Logger.debug(filter2);
			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			
			YmtOnlinecs info = YmtSellerCSIMWapper.selectYmtOnlinecs(null, apploginBean.getUserId()).get(0);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			Logger.verifyEquals(apploginBean.getUserName()==null?new JUserWapper().selectUserinfoByUid(csid).get(0).getLoginId():apploginBean.getUserName(), info.getCSName(), "验证csname");
			
			Logger.verifyEquals(3, info.getLoginStatus(), "验证loginStatus为都在线3");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
			SellerCSIMCallService.OffLine(csid, token);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppLogin_007() {
		Logger.start(true,"App重复登入成功");
		try {
			Integer csid=30245493;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			apploginBean.setUserId(csid);
			apploginBean.setUserName("test");
			apploginCall.setData(apploginBean);
			apploginCall.callService();			
			apploginCall.callService();			
			Logger.verifyIsContains("true", apploginCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", apploginCall.getString("Status"), "验证Status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppLogin_008() {
		Logger.start(true,"输入userid0返回失败");
		try {
			apploginBean.setUserId(0);
			apploginCall.setData(apploginBean);
			apploginCall.callService();			
			Logger.verifyIsContains("200", apploginCall.getString("Status"), "验证Status");
			Logger.verifyIsContains("false", apploginCall.getString("Result"), "验证IsSuccess");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}